﻿using System;
using System.Web;
using System.Web.UI;
using YawetagLib.Utilities;

namespace YawetagLib.Web.Utilities
{
    public static class PersistenceHelper
    {
        public static T GetHttpContextData<T>(HttpContextBase httpContext, string id, T defaultValue)
        {
            return httpContext.Items.Contains(id) ? (T)httpContext.Items[id] : SetHttpContextData(httpContext, id, defaultValue);
        }

        public static T GetHttpContextData<T>(HttpContextBase httpContext, string id)
        {
            return GetHttpContextData(httpContext, id, default(T));
        }

        public static T GetHttpContextData<T>(HttpContextBase httpContext, string id, Func<T> initializer)
        {
            return httpContext.Items.Contains(id) ? (T)httpContext.Items[id] : SetHttpContextData(httpContext, id, initializer());
        }

        public static T SetHttpContextData<T>(HttpContextBase httpContext, string id, T value)
        {
            httpContext.Items[id] = value;

            return value;
        }

        public static T GetViewStateData<T>(StateBag viewState, string identifier, Func<T> initializer)
        {
            return viewState.Keys.Contains(identifier) ? (T)viewState[identifier] : SetViewStateData(viewState, identifier, initializer());
        }

        public static T GetViewStateData<T>(StateBag viewState, string identifier, T defaultValue)
        {
            return GetViewStateData(viewState, identifier, () => defaultValue);
        }

        public static T GetViewStateData<T>(StateBag viewState, string identifier)
        {
            return GetViewStateData(viewState, identifier, default(T));
        }

        public static T SetViewStateData<T>(StateBag viewState, string identifier, T value)
        {
            viewState[identifier] = value;

            return value;
        }

        public static T GetSessionData<T>(HttpContextBase httpContext, string identifier, Func<T> initializer)
        {
            return httpContext.Session.Keys.Contains(identifier)
                ? (T)httpContext.Session[identifier]
                : SetSessionData(httpContext, identifier, initializer());
        }

        public static T GetSessionData<T>(HttpContextBase httpContext, string identifier, T defaultValue)
        {
            return GetSessionData(httpContext, identifier, () => defaultValue);
        }

        public static T GetSessionData<T>(HttpContextBase httpContext, string identifier)
        {
            return GetSessionData(httpContext, identifier, default(T));
        }

        public static T SetSessionData<T>(HttpContextBase httpContext, string identifier, T value)
        {
            httpContext.Session[identifier] = value;

            return value;
        }

        public static void RemoveSessionData(HttpContextBase httpContext, string identifier)
        {
            httpContext.Session.Remove(identifier);
        }

        public static string GetCookieData(HttpContextBase httpContext, string identifier, string defaultValue)
        {
            HttpCookie cookie = httpContext.Request.Cookies.Get(identifier);

            return cookie != null ? cookie.Value : SetCookieData(httpContext, identifier, defaultValue).Value;
        }

        public static HttpCookie SetCookieData(HttpContextBase httpContext, string identifier, string value)
        {
            HttpCookie cookie = httpContext.Request.Cookies.Get(identifier);
            value = HttpUtility.HtmlEncode(value);

            if (cookie != null) 
            { 
                cookie.Value = value; 
            }
            else
            {
                cookie = new HttpCookie(identifier, value);
                httpContext.Response.Cookies.Add(cookie);
            }

            return cookie;
        }
    }
}